﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using HMP.DA;
using HMP.BL;
using System.Data.Objects;
namespace HMP.GUI
{
    public partial class MainForm1 : Form
    {
        public MainForm1()
        {
            //MainForm1 frm = new MainForm1();
            //frm.Hide();
            InitializeComponent();
            //LoginForm form = new LoginForm();
            //form.ShowDialog();
        }

        public SqlConnection con;
        
        
        /// <summary>
        /// Module System Management
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void navBaruserlist_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcUserlist.Show();
            grcUserInformation.Hide();
            grcChangepass.Hide();
            grcProvince.Hide();
            grcClinicUnit.Hide();
            grcDistrict.Hide();
        }

      
        private void navBarCreateUser_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcUserlist.Hide();
            grcUserInformation.Show();
            grcChangepass.Hide();
            grcProvince.Hide();
            grcClinicUnit.Hide();
            grcDistrict.Hide();
        }

        private void navBarChangepass_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcUserlist.Hide();
            grcUserInformation.Hide();
            grcChangepass.Show();
            grcProvince.Hide();
            grcClinicUnit.Hide();
            grcDistrict.Hide();
        }

        private void navBarProvince_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcUserlist.Hide();
            grcUserInformation.Hide();
            grcChangepass.Hide();
            grcProvince.Show();
            grcClinicUnit.Hide();
            grcDistrict.Hide();
        }

        private void btnAdduser_Click(object sender, EventArgs e)
        {
            grcUserlist.Hide();
            grcUserInformation.Show();
            grcChangepass.Hide();
            grcProvince.Hide();
            grcClinicUnit.Hide();
            grcDistrict.Hide();
        }

        private void btnAddProvince_Click(object sender, EventArgs e)
        {
            NewCategoryItemForm1 form1 = new NewCategoryItemForm1();
            form1.ShowDialog();
        }

      
       

        public static void Insertprovince(string matinh, string tentinh)
        {
            //using (QUAN_LI_THONG_TIN_BENH_NHANEntities1 dk = new QUAN_LI_THONG_TIN_BENH_NHANEntities1())
            //{
            //    tbProvince province = new tbProvince();
            //    province.ProvinceID = matinh;
            //    province.ProvinceName = tentinh;
            //    dk.AddTotbProvinces(province);
            //    dk.SaveChanges();
            //}
        }

        private void navBarDistrict_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcUserlist.Hide();
            grcUserInformation.Hide();
            grcChangepass.Hide();
            grcProvince.Hide();
            grcClinicUnit.Hide();
            grcDistrict.Show();
        }

        private void navBarClinicUnit_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcUserlist.Hide();
            grcUserInformation.Hide();
            grcChangepass.Hide();
            grcProvince.Hide();
            grcClinicUnit.Show();
            grcDistrict.Hide();
        }

       
        /////
        /////Module Patient Management
        /////
        //su kien nut add hien form tiep nhan benh nhan
        private void btnAddPatient_Click(object sender, EventArgs e)
        {
            grcPatientList.Hide();
            grcRegisterhealthcare.Show();
            grcReportbyPlacenTime.Hide();
            grcQuantityOfHealthCare.Hide();
            grcReportbyStaff.Hide();
            grcReportByReceptionUnit.Hide();
            grcQuantityOfHealthCare.Hide();
        }

        //hien form danh sach benh nhan
        private void navBarPatientList_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcPatientList.Show();
            grcRegisterhealthcare.Hide();
            grcReportbyPlacenTime.Hide();
            grcQuantityOfHealthCare.Hide();
            grcReportbyStaff.Hide();
            grcReportByReceptionUnit.Hide();
            grcQuantityOfHealthCare.Hide();
        }
        //hien form tiep nhan benh nhan
        private void navBarRegisterHealthCare_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcPatientList.Hide();
            grcRegisterhealthcare.Show();
            grcReportbyPlacenTime.Hide();
            grcQuantityOfHealthCare.Hide();
            grcReportbyStaff.Hide();
            grcReportByReceptionUnit.Hide();
            grcQuantityOfHealthCare.Hide();
        }
        //hien form report theo thoi gian va phong kham,tinh,thanh pho
        private void navBarClinicProvinceCity_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcPatientList.Hide();
            grcRegisterhealthcare.Hide();
            grcReportbyPlacenTime.Show();
            grcQuantityOfHealthCare.Hide();
            grcReportbyStaff.Hide();
            grcReportByReceptionUnit.Hide();
            grcQuantityOfHealthCare.Hide();
        }
        //hien form report theo don vi tiep nhan
        private void navBarReceptionUnit_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcPatientList.Hide();
            grcRegisterhealthcare.Hide();
            grcReportbyPlacenTime.Hide();
            grcQuantityOfHealthCare.Hide();
            grcReportbyStaff.Hide();
            grcReportByReceptionUnit.Show();
            grcQuantityOfHealthCare.Hide();
        }
        //hien form report theo nhan vien
        private void navReceptionStaff_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcPatientList.Hide();
            grcRegisterhealthcare.Hide();
            grcReportbyPlacenTime.Hide();
            grcQuantityOfHealthCare.Hide();
            grcReportbyStaff.Show();
            grcReportByReceptionUnit.Hide();
            grcQuantityOfHealthCare.Hide();
        }

        //hien form report theo so luong dang ky
        private void navBarQuantityofHealthCare_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grcPatientList.Hide();
            grcRegisterhealthcare.Hide();
            grcReportbyPlacenTime.Hide();
            grcQuantityOfHealthCare.Hide();
            grcReportbyStaff.Hide();
            grcReportByReceptionUnit.Hide();
            grcQuantityOfHealthCare.Show();
        }

        private void MainForm1_Load(object sender, EventArgs e)
        {
            LoadData();
            }

        //load du lieu
        private void LoadData() {
            QLTTBNEntities context = new QLTTBNEntities();
            var qprovince = context.tbProvinces;
            var qusers = context.tbUsers;
            var qdistrict = context.tbDistricts;
            var qpatients = context.tbPatients;
            var province = from p in context.tbProvinces select p.ProvinceName;
            var clinic = from a in context.tbUnits select a.ClinicName;
            
            grdProvince.DataSource = qprovince.ToList();
            grdUserList.DataSource = qusers.ToList();
            grdDistrict1.DataSource = qdistrict.ToList();
            grdPatientList.DataSource = qpatients.ToList();
            cmbProvince.DataSource = province.ToList();
            cmbByPlace.DataSource = province.ToList();
            cmbListReport.DataSource = clinic.ToList();
        }
     
        //Xu li cho nut AddUser
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            CheckDataExist ex = new CheckDataExist();
            if (ex.KiemTra(txtTelephone.Text))
            {
                QLTTBNEntities dk = new QLTTBNEntities();
                tbUser user = new tbUser();
                user.UserID = txtUserID.Text;
                user.UserName = txtUserName.Text;
                user.Email = txtemail.Text;
                user.Fullname = txtFullname.Text;
                user.Address = txtAddress.Text;
                user.Password = txtPassword.Text;
                user.GroupUser = cmbGroupUser.Text;
                user.Telephone = int.Parse(txtTelephone.Text);
                MessageBox.Show("Them Thanh Cong");
                dk.tbUsers.Add(user);
                dk.SaveChanges();
                LoadData();
            }
            else
            {
                MessageBox.Show("Trung Lap Du Lieu", "Canh Bao");
            }
            
        }

        //Bien mat ID trong textbox IDUser
        private void txtUserID_Click_1(object sender, EventArgs e)
        {
            if (txtUserID.Text == "ID")
                txtUserID.Text = "";
        }
        private void navBarCreateUser_LinkClicked_1(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (txtUserID.Text == "")
                txtUserID.Text = "ID";
        }

        private void cmbDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            QLTTBNEntities context = new QLTTBNEntities();
            var district = from p in context.tbDistricts where p.tbProvince.ProvinceName == cmbProvince.Text select p.DistrictName;
            cmbDistrict.DataSource = district.ToList();
            LoadData();
        }

        private void cmbClinicReport_SelectedIndexChanged(object sender, EventArgs e)
        {
            string x = "PK";
            QLTTBNEntities context = new QLTTBNEntities();
            var clinic = from p in context.tbUnits where p.Clinic.Contains(x) select p.ClinicName;
            cmbDistrict.DataSource = clinic.ToList();
        }

        private void btnEditProvince_Click(object sender, EventArgs e)
        {
            frmCategoryInformation EditCategory = new frmCategoryInformation();
            EditCategory.Show();
        }


        private void grdUserList_DoubleClick(object sender, EventArgs e)
        {
            string sID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "UserID").ToString();
            grcUserInformation.Show();
            
        }

        private void grcUserInformation_Paint(object sender, PaintEventArgs e)
        {
            txtUserID.Text = "1";
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            string sID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "UserID").ToString();
            grcUserInformation.Show();
        }

        private void grdUserList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string sID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "UserID").ToString();
            grcUserInformation.Show();
        }

        private void gridView9_DoubleClick(object sender, EventArgs e)
        {
            string sID = gridView9.GetRowCellValue(gridView9.FocusedRowHandle, "UserID").ToString();
            grcUserInformation.Show();
        }

        private void cmbProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            QLTTBNEntities context = new QLTTBNEntities();
            var district = from p in context.tbDistricts where p.tbProvince.ProvinceName == cmbProvince.Text select p.DistrictName;
            cmbDistrict.DataSource = district.ToList();
        }



    }
}
